package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.ReleaseSupplyGoodsListBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;


/**
 * 发布资源-修改-列表
 * Created by ShineF on 2017/10/28 0028.
 */

public class ReleaseSupplyListRecycleAdapter extends RecyclerView.Adapter<ReleaseSupplyListRecycleAdapter.SupplyHolder> {

    private Context mContext;
    private ArrayList<ReleaseSupplyGoodsListBean.ListBean> mDataList;
    private int mHeight = -1;
    private ItemClickListener mListener;
    private String mType;

    public ReleaseSupplyListRecycleAdapter(Context context, ArrayList<ReleaseSupplyGoodsListBean.ListBean> dataList, int height, String type) {
        this.mContext = context;
        this.mDataList = dataList;
        this.mHeight = height;
        this.mType = type;
    }

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mListener = listener;
    }


    @Override
    public SupplyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SupplyHolder(View.inflate(mContext, R.layout.adapter_release_supply_list_recycle, null));
    }

    @Override
    public void onBindViewHolder(SupplyHolder holder, final int position) {
        final ReleaseSupplyGoodsListBean.ListBean releaseSupplyListBean = mDataList.get(position);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mLlItem.getLayoutParams();
        if (params != null) {
            params.height = (mHeight - ConvertUtils.dip2px(mContext, 13 * 7)) / 7;
            holder.mLlItem.setLayoutParams(params);
        }
        if (!TextUtils.isEmpty(releaseSupplyListBean.getDemandSignName())) {
            holder.mTvSupplyName.setText(releaseSupplyListBean.getDemandSignName());
        }
        if (!TextUtils.isEmpty(releaseSupplyListBean.getCreateTime())) {
            holder.mTvReleaseTime.setText(StringUtils.getResourceStr(mContext, R.string.edit_release_start_time, releaseSupplyListBean.getCreateTime().substring(0
                    , releaseSupplyListBean.getCreateTime().length() - 3)));
        }
        if (!TextUtils.isEmpty(releaseSupplyListBean.getStartValidTime())) {
            holder.mTvOperationTime.setText(StringUtils.getResourceStr(mContext, R.string.edit_release_end_time, releaseSupplyListBean.getStartValidTime().substring(0
                    , releaseSupplyListBean.getStartValidTime().length() - 3)));
        }
        if (20 == (releaseSupplyListBean.getContractStatus()) && ContractConstant.RELEASE_SUPPLY_INFO.equals(mType)) {
            holder.mTvIsEffective.setVisibility(View.VISIBLE);
        } else {
            holder.mTvIsEffective.setVisibility(View.GONE);
        }
        holder.mLlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.clickListener(position, releaseSupplyListBean, view);
                }
            }
        });
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public interface ItemClickListener {
        void clickListener(int i, ReleaseSupplyGoodsListBean.ListBean bean, View view);
    }

    class SupplyHolder extends RecyclerView.ViewHolder {
        TextView mTvSupplyName;
        TextView mTvReleaseTime;
        TextView mTvOperationTime;
        private LinearLayout mLlItem;
        private TextView mTvIsEffective;

        SupplyHolder(View itemView) {
            super(itemView);
            mTvSupplyName = (TextView) itemView.findViewById(R.id.tv_supply_name);
            mTvReleaseTime = (TextView) itemView.findViewById(R.id.tv_release_time);
            mTvOperationTime = (TextView) itemView.findViewById(R.id.tv_operation_time);
            mLlItem = (LinearLayout) itemView.findViewById(R.id.item_content);
            mTvIsEffective = (TextView) itemView.findViewById(R.id.tv_is_effective);
        }
    }
}
